package com.weilele.leisure.time.ui.holder

import android.view.View
import com.weilele.leisure.time.bean.VideoListBean
import com.weilele.leisure.time.databinding.HolderVideoItemHistoryBinding
import com.weilele.leisure.time.ui.module.video_detail.VideoDetailFragment
import com.weilele.leisure.time.ui.module.video_detail.copyData
import com.weilele.mvvm.adapter.MvvmHolder
import com.weilele.mvvm.adapter.mvvmRcvAdapter
import com.weilele.mvvm.utils.activity.SpanBean
import com.weilele.mvvm.utils.activity.onClick
import com.weilele.mvvm.utils.activity.textFrom
import com.weilele.mvvm.utils.activity.textFromSpanBean
import com.weilele.mvvm.utils.getCountTimeByLong
import com.weilele.mvvm.utils.longTimeToStr
import com.weilele.leisure.time.bean.db.HistoryVideoBean
import com.weilele.mvvm.utils.glide.setImageAny

class HistoryVideoHolder : MvvmHolder<HistoryVideoBean, HolderVideoItemHistoryBinding> {
    companion object {
        fun newInstance() =
            mvvmRcvAdapter(HistoryVideoHolder::class.java, ::onVideoSame, ::onVideoSame)

        private fun onVideoSame(old: HistoryVideoBean?, new: HistoryVideoBean?): Boolean {
            return old?.dbId == new?.dbId
                    && old?.videoId == new?.videoId
                    && old?.playTitle == new?.playTitle
                    && old?.playUrl == new?.playUrl
                    && old?.currentDuration == new?.currentDuration
                    && old?.totalDuration == new?.totalDuration
        }
    }

    init {
        itemView.onClick {
            dataWhenBind?.let { historyBean ->
                VideoDetailFragment.start(it,
                    VideoListBean.RowsDTO().apply {
                        copyData(historyBean)
                    })
            }
        }
    }

    constructor(binding: HolderVideoItemHistoryBinding) : super(binding)
    constructor(itemView: View) : super(itemView)

    override fun bindData(data: HistoryVideoBean) {
        mBinding.name.textFrom(data.title)
        mBinding.ivCover.setImageAny(data.picUrl)
        mBinding.tvProgress.textFromSpanBean(
            mutableListOf(
                SpanBean(data.playTitle, textSize = 13),
                SpanBean(
                    "\n${getCountTimeByLong(data.currentDuration)}/${getCountTimeByLong(data.totalDuration)}\n${
                        longTimeToStr(
                            data.updateTime
                        )
                    }",
                    textSize = 11
                ),
            )
        )
    }
}